#include<Windows.h>
#include<GL\GL.h>
#include<GL\GLU.h>
#include<GL\glut.h>
#include<stdio.h>
#include<math.h>
#include<string>
#include<cstdlib>
#include<vector>
#include<time.h>
#include"image.h"
#include"DrawImage.h"
#include"tDisplay.h"
#include"sound.h"
#include"Continent.h"
using namespace std;

int v=0 , h=0 , sx=1 , sy=1 , theta=0 , WindowX , WindowY ;
#define PI 3.14159265
int ind=0 , currentWindow=-2 , currentCont=0 , Load_num=0, char_num=0 ;
Image *map_photos[12], *startButton , *Scores[11] , *Loadd[4] , *HowToPlayButton , *ExitButton, *back;
Image *chars[4] ;
pair < Image *, int> Questions[11];
int displaiedFrame=0;
tDisplay *a;
Continent *Con;
string c_num_to_name[6] ;

void myStyleInit()
{
	glClearColor(1.0,1.0,1.0,0.0);
	glPointSize(4.0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0,800,0,600,0,-1);
	glClear( GL_COLOR_BUFFER_BIT );
}

void LoadLoad()
{
	a = new tDisplay();
	a->displayLoad(0);
	Loadd[1]=new Image("load2.bmp");
	HowToPlayButton=new Image("howtoplay.bmp");
	a->displayLoad(1);
	Loadd[2]=new Image("load3.bmp");
	ExitButton = new Image("exit.bmp");
	a->displayLoad(2);
	Loadd[3]=new Image("load4.bmp");
	back = new Image("back.bmp");
	a->displayLoad(3);
	chars[0]= new Image("_amr.bmp");
	chars[1]= new Image("__hassan.bmp");
	a->displayLoad(0);
	chars[2]= new Image("_tamer.bmp");
	chars[3]= new Image("_iten.bmp");
	a->displayLoad(1);
}

void Load ()
{
	a->displayLoad(2);
	Con=new Continent();
	char x[11];
	string str;
	for ( int i=0 ; i<=100 ; i+=10 )
	{
		sprintf(x,"%d",i);
		str = x ;
		Scores[i]=new Image("score-"+str+".bmp");
	}
	a->displayLoad(3);
	map_photos[0]=new Image("worldMap.bmp");
	map_photos[1]=new Image("africa.bmp");
	a->displayLoad(0);
	map_photos[2]=new Image("asia.bmp");
	map_photos[3]=new Image("europe.bmp");
	a->displayLoad(1);
	map_photos[4]=new Image("namerica.bmp");
	map_photos[5]=new Image("samerica.bmp");
	a->displayLoad(2);
	map_photos[6]=new Image("africa_info.bmp");
	map_photos[7]=new Image("asia_info.bmp");
	a->displayLoad(3);
	map_photos[8]=new Image("europe_info.bmp");
	map_photos[9]=new Image("nam_info.bmp");
	a->displayLoad(0);
	map_photos[10]=new Image("sam_info.bmp");
	map_photos[11]=new Image("continue.bmp");
	a->displayLoad(1);
	startButton  =new Image("start.bmp");
}

void myDisplay()
{
	if ( currentWindow==-2 )
	{
		currentWindow=-1;
		LoadLoad();
		Load();
		myDisplay();
	}
	else if ( currentWindow==-1 )
		a->displayFrame1(ind,0);
	else if ( currentWindow==0 )
		;
	else if ( currentWindow==1 )
		a->displayFrame2(ind,currentCont);
	
	displaiedFrame=1;
}

void mouseClick( int button , int state , int x , int y )
{
	if ( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN ) // there is a click
	{
		if ( currentWindow==-1 ) // home page
		{
			if ( x >= 410 && (600-y) >= 150 && x <= 580 && (600-y) <= 210 ) // start button
			{
				a->displayChar();
				currentWindow=0;
				//a->displayFrame2(ind,0);
				//displaiedFrame=2;
			}
		}
		else if ( currentWindow==0 ) // choose character
		{
			if ( x >= 100 && (600-y) >= 230 && x <= 240 && (600-y) <= 385 ) // char 1
			{
				char_num=0;
				currentWindow=1;
				a->displayFrame2(ind,0);
			}
			else if ( x >= 250 && (600-y) >= 230 && x <= 390 && (600-y) <= 385 ) // char 2
			{
				char_num=1;
				currentWindow=1;
				a->displayFrame2(ind,0);
			}
			else if ( x >= 400 && (600-y) >= 230 && x <= 540 && (600-y) <= 385 ) // char 3
			{
				char_num=2;
				currentWindow=1;
				a->displayFrame2(ind,0);
			}
			else if ( x >= 550 && (600-y) >= 230 && x <= 690 && (600-y) <= 385 ) // char 4
			{
				char_num=3;
				currentWindow=1;
				a->displayFrame2(ind,0);
			}
		}
		else if ( currentWindow==1 ) // choosing page
		{
			if ( x >= 340 && x<=530 && (600-y)<=416 && (600-y)>=210 ) // africa
			{
				currentCont=1;
				currentWindow=2;
				a->displayFrame3(currentCont);
			}
			else if ( x>=530 && x<=760 && (600-y)<=465 && (600-y)>=210) // asia
			{
				currentCont=2;
				currentWindow=2;
				a->displayFrame3(currentCont);
			}
			else if ( x>=350 && x<=780 && (600-y)<=600 && (600-y)>=465) // europe
			{
				currentCont=3;
				currentWindow=2;
				a->displayFrame3(currentCont);
			}
			else if ( x>=0 && x<=345 && (600-y)<=585 && (600-y)>=330) // north america
			{
				currentCont=4;
				currentWindow=2;
				a->displayFrame3(currentCont);
			}
			else if ( x>=180 && x<=315 && (600-y)<=330 && (600-y)>=110) // south america
			{
				currentCont=5;
				currentWindow=2;
				a->displayFrame3(currentCont);
			}
		}
		else if ( currentWindow==2 ) // start Qara
		{
			if ( x>=30 && x<=105 && (600-y)<=112 && (600-y)>=50 )
			{
				Con->newContinent(currentCont);
				Con->showQuestion(0,0,char_num);
				currentWindow=3;
			}
		}
		else if ( currentWindow==3 ) // el as2ela
		{

		}
	}
}

void motionFunction(int x, int y)
{
	if ( currentWindow!=1 )
	{
		//currentCont=0;
		glutPassiveMotionFunc(motionFunction);
		return ;
	}
	else if ( currentWindow == 1 )
	{
		if ( x >= 340 && x<=530 && (600-y)<=416 && (600-y)>=210 ) // africa
		{
			currentCont=1;
			a->displayFrame2(ind,currentCont);
		}
		else if ( x>=530 && x<=760 && (600-y)<=465 && (600-y)>=210) // asia
		{
			currentCont=2;
			a->displayFrame2(ind,currentCont);
		}
		else if ( x>=350 && x<=780 && (600-y)<=600 && (600-y)>=465) // europe
		{
			currentCont=3;
			a->displayFrame2(ind,currentCont);
		}
		else if ( x>=0 && x<=345 && (600-y)<=585 && (600-y)>=330) // north america
		{
			currentCont=4;
			a->displayFrame2(ind,currentCont);
		}
		else if ( x>=180 && x<=315 && (600-y)<=330 && (600-y)>=110) // south america
		{
			currentCont=5;
			a->displayFrame2(ind,currentCont);
		}
		else
		{
			currentCont=0;
			a->displayFrame2(ind,currentCont);
		}
	}
}

int main(int argc, char **argv)
{
	Sound *snd;
	snd = new Sound("music.wav", "myfile");
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	WindowX=800 ;
	WindowY=600 ;
	glutInitWindowSize(WindowX,WindowY);
	glutInitWindowPosition(50,50);
	glutCreateWindow("OpenGL Template");
	//glutFullScreen();
	myStyleInit();
	Loadd[0]=new Image("load1.bmp");
	glutDisplayFunc(myDisplay);
	snd->Play();
	

	//glutKeyboardFunc(keyboard);
	//glutTimerFunc(50,checkLoad,0);
	glutMouseFunc(mouseClick);
	glutPassiveMotionFunc(motionFunction);
	glutMainLoop();
	return 0;
}